<?php
namespace IFI\Symfony2Bundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use IFI\Symfony2Bundle\Entity\Article;
use IFI\Symfony2Bundle\Entity\Auteur;
use IFI\Symfony2Bundle\Entity\Tag;
use Symfony\Component\HttpFoundation\Request;

class ReseauSocialController extends Controller
{

    public function accueilAction()
    {        
        $em = $this->getDoctrine()->getManager();        
        $articleRepo = $em->getRepository('IFISymfony2Bundle:Article');
		
        $request = $this->getRequest();
		$pageID = $request->query->get('page');
		if(!isset($pageID)){
			$pageID = 1;
		}
		$count = ceil($articleRepo->count()/3);
		
        $articles = $articleRepo->latest(($pageID-1)*3, 3);
	
        return $this->render('IFISymfony2Bundle:ReseauSocial:index.html.twig', array("articles" => $articles,
												"currentPage" => $pageID,
											"pageCount" => $count));
    }
    
    public function loginAction()
    {        
        return $this->render('IFISymfony2Bundle:ReseauSocial:login.html.twig');
    }
    
    public function articleAction($idArticle){
        $em = $this->getDoctrine()->getManager();
        $articleRepo = $em->getRepository('IFISymfony2Bundle:Article');
		
        $article = $articleRepo->find($idArticle);

        return $this->render('IFISymfony2Bundle:ReseauSocial:article.html.twig', array('article' => $article));
    }
    
    public function auteurAction($loginAuteur){
        $em = $this->getDoctrine()->getManager();
        $articleRepo = $em->getRepository('IFISymfony2Bundle:Article');
        
        $request = $this->getRequest();
        $pageID = $request->query->get('page');
		if(!isset($pageID)){
			$pageID = 1;
		}
		$count = ceil($articleRepo->count()/3);
	
        
        $articles = $articleRepo->findByAuteur($loginAuteur, ($pageID-1)*3, 3);
		
        return $this->render('IFISymfony2Bundle:ReseauSocial:index.html.twig', array('articles' => $articles, "currentPage" => $pageID,
											"pageCount" => $count));
    }
    
    public function dateAction($datePost){        
        $em = $this->getDoctrine()->getManager();
        $articleRepo = $em->getRepository('IFISymfony2Bundle:Article');
        $articles = $articleRepo->findByDate($datePost);
        
        return $this->render('IFISymfony2Bundle:ReseauSocial:index.html.twig', array('articles' => $articles, "currentPage" => 0, "pageCount" => 0));               
    }
    
    public function tagsAction($tags){
        $tagsArray = explode("-", $tags);       
        
        $em = $this->getDoctrine()->getManager();
        $articleRepo = $em->getRepository('IFISymfony2Bundle:Article');
        $articles = $articleRepo->findByTags($tagsArray);
        
        return $this->render('IFISymfony2Bundle:ReseauSocial:index.html.twig', array('articles' => $articles, "currentPage" => 0, "pageCount" => 0));
    }

    public function postAction(Request $request)
    {
        $article = new Article();

		$form = $this->createFormBuilder($article)
			->add('titre', 'text')//<input type="text">
    		->add('contenu', 'textarea')//suite de <select>
			->add('tags', 'text')//<input type="submit">
    		->add('save', 'submit')//encore un autre
			->getForm();
	
		$form->handleRequest($request);
	
		if ($form->isSubmitted()) {
			if($form->isValid()){
				$em = $this->getDoctrine()->getManager();
				
				$user = $this->get('security.context')->getToken()->getUser();
								
				$article_to_add = $form->getData();
				$tagRepo = $em->getRepository('IFISymfony2Bundle:Tag');

				$tags_array = explode("-", $form->getData()->getTags());
					
				$tags = array();
			
				for($i = 0; $i<count($tags_array); $i++){
					$string_tag_i = $tags_array[$i];
					if( ($tag_i = $tagRepo->findOneByNom($string_tag_i)) == null) {
						$tag_i = new Tag();
						$tag_i->setNom($string_tag_i);
						if(!in_array($tag_i, $tags)){
							array_push($tags, $tag_i);
							$em->persist($tag_i);
			        		$em->flush();
						}
					}else{
						array_push($tags, $tag_i);
					}
				}
				
				$article_to_add->setAuteur($user);
				$article_to_add->setTags($tags);
				
				$em->persist($article_to_add);
        		$em->flush();
        		
				return $this->render('IFISymfony2Bundle:ReseauSocial:post_success.html.twig');
			}else{
		        return $this->redirect($this->generateUrl('post_fail'));
			}
		}else{
			return $this->render('IFISymfony2Bundle:ReseauSocial:post.html.twig', array('form' => $form->createView()));
		}
    }

    public function post_successAction(){
		return $this->render('IFISymfony2Bundle:ReseauSocial:post_success.html.twig');
    }

    public function post_failAction(){
		return $this->render('IFISymfony2Bundle:ReseauSocial:post_fail.html.twig');
    }
}
?>
